package net.sx.minemodule.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.OrientationHelper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineItemRefundDetailStepBinding;
import net.sx.minemodule.mvp.bean.EnumRefundType;
import net.sx.minemodule.mvp.bean.EnumSchedule;
import net.sx.minemodule.mvp.item.RefundStepItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator 可爱的路人 on 2019/7/5.
 * Email:513421345@qq.com
 * 售后订单，进度控件
 */
public class MineRefundStepView extends LinearLayout {
    //默认状态为已申请
    private int current = EnumSchedule.WAITING_REQUEST.getKey();
    private List<MineItemRefundDetailStepBinding> stepItems;
    //完成状态颜色
    private int completedColor;
    //未完成状态颜色
    private int nextColor;
    private int completedDrawable = R.mipmap.progress_sel;
    private int nextDrawable = R.mipmap.progress_unselected;
    //默认的退款
    private int type = EnumRefundType.REFUND.getKey();
    private boolean isDelete = false;//是否撤回申请

    public MineRefundStepView(@NonNull Context context) {
        super(context);
        init();
    }

    public MineRefundStepView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public MineRefundStepView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();

    }

    /**
     * 设置进度的数据
     *
     * @param data
     */
    public void setStepData(List<EnumSchedule> data) {
        if (data != null) {
            removeAllViews();
            stepItems = new ArrayList<>();
            EnumSchedule scheduleCurrent = EnumSchedule.getSchedule(current, type);

            for (int i1 = 0; i1 < data.size(); i1++) {
                EnumSchedule verifyStatus = data.get(i1);
                RefundStepItem refundStepItem;
                //退款
                if (type == EnumRefundType.REFUND.getKey()) {
                    if (verifyStatus == EnumSchedule.REFUND_REQUEST) {
                        //第一步 直接点亮
                        refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                    } else if ((scheduleCurrent == EnumSchedule.REJECT_REQUEST || scheduleCurrent == EnumSchedule.REFUND_REJECT_20 || scheduleCurrent == EnumSchedule.REFUND_REJECT_8
                            || scheduleCurrent.getKey() >= EnumSchedule.REFUND_WAIT.getKey())
                            && verifyStatus == EnumSchedule.REFUND_WAIT) {
                        //第二步 驳回或者大于7 点亮
                        refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                    } else if(isDelete()){
                        refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                    } else if (verifyStatus == EnumSchedule.REFUND_SUCCESS) {
                        //第三步
                        //1.正常完成 点亮
                        if (scheduleCurrent == EnumSchedule.REFUND_SUCCESS) {
                            refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                        } else if (scheduleCurrent == EnumSchedule.REFUND_VERIFY_FINISH) {
                            //2. 等待退款，横线加深
                            refundStepItem = (new RefundStepItem(verifyStatus, completedColor, nextColor, nextDrawable));
                        } else if (scheduleCurrent == EnumSchedule.REJECT_REQUEST || scheduleCurrent == EnumSchedule.REFUND_REJECT_20 || scheduleCurrent == EnumSchedule.REFUND_REJECT_8) {
                            //驳回 点亮
                            refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                        } else {
                            refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                        }

                    } else {
                        refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                    }

                } else {
                    //退款退货
                    if (verifyStatus == EnumSchedule.RETURN_GOODS_REQUEST) {
                        //第一步 直接点亮
                        refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                    } else if (verifyStatus == EnumSchedule.RETURN_GOODS_WAIT_RECEIVE
                            && (scheduleCurrent.getKey() >= EnumSchedule.RETURN_GOODS_WAIT_RECEIVE.getKey()
                            || scheduleCurrent == EnumSchedule.REJECT_REQUEST
                            || scheduleCurrent == EnumSchedule.RETURN_GOODS_REJECT_20
                            || scheduleCurrent == EnumSchedule.RETURN_GOODS_REJECT_6
                    )) {
                        //第二步 驳回或者大于3 点亮
                        refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                    } else if (isDelete() || scheduleCurrent == EnumSchedule.WAITING_REQUEST_10 || scheduleCurrent == EnumSchedule.WAITING_REQUEST_11) {
                        //后面两个如果是审核中都直接灰色
                        refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                    } else if (verifyStatus == EnumSchedule.RETURN_GOODS_WAIT_REFUND_5) {
                        //第三步
                        //1. 卖家收到商品 横线加深 （待收货和平台驳回）
                        if (scheduleCurrent == EnumSchedule.RETURN_GOODS_WAIT_CHECK || scheduleCurrent == EnumSchedule.REJECT_REQUEST) {
                            refundStepItem = (new RefundStepItem(verifyStatus, completedColor, nextColor, nextDrawable));
                        } else if (scheduleCurrent.getKey() > EnumSchedule.RETURN_GOODS_WAIT_CHECK.getKey()
                                || scheduleCurrent == EnumSchedule.RETURN_GOODS_REJECT_20
                                || scheduleCurrent == EnumSchedule.RETURN_GOODS_REJECT_6) {
                            //2. 大于4 或者收到商品驳回 点亮，上面过滤了10 11 审核中
                            refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                        } else {
                            refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                        }
                    } else if (verifyStatus == EnumSchedule.RETURN_GOODS_SUCCESS) {
                        //第四步
                        //1. 等待退款 横线加深
                        if (scheduleCurrent == EnumSchedule.RETURN_GOODS_WAIT_REFUND_5 || scheduleCurrent == EnumSchedule.RETURN_GOODS_WAIT_REFUND_6) {
                            refundStepItem = (new RefundStepItem(verifyStatus, completedColor, nextColor, nextDrawable));
                        } else if (scheduleCurrent == EnumSchedule.RETURN_GOODS_SUCCESS) {
                            //2. 完成
                            refundStepItem = new RefundStepItem(verifyStatus, completedColor, completedColor, completedDrawable);
                        } else if (scheduleCurrent == EnumSchedule.REJECT_REQUEST
                                || scheduleCurrent == EnumSchedule.RETURN_GOODS_REJECT_20
                                || scheduleCurrent == EnumSchedule.RETURN_GOODS_REJECT_6) {
                            //3. 驳回 线灰色
                            refundStepItem = (new RefundStepItem(verifyStatus, nextColor, completedColor, completedDrawable));
                        } else {
                            refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                        }
                    } else {
                        refundStepItem = (new RefundStepItem(verifyStatus, nextColor, nextColor, nextDrawable));
                    }
                }

                if (i1 == 0) {
                    refundStepItem.setLineVisible(false);
                }
                MineItemRefundDetailStepBinding v = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                        refundStepItem.getLayout(), this, true);
                stepItems.add(v);
                View root = v.getRoot();
                LayoutParams lp = ((LinearLayout.LayoutParams) root.getLayoutParams());
                if (i1 == 0) {
                    lp.weight = 0;
                } else {
                    lp.weight = 1;
                }

                v.setItem(refundStepItem);
                v.executePendingBindings();
            }
        }
    }

    /**
     * 当前订单处于哪个进度
     *
     * @param status {@link net.sx.minemodule.mvp.bean.EnumSchedule}
     */
    public void setCurrentStep(int status) {
        current = status;
        if (stepItems == null) {
            return;
        }
        for (MineItemRefundDetailStepBinding r : stepItems) {
            r.invalidateAll();
        }
    }

    /**
     * 售后类型 {@link EnumRefundType}
     *
     * @param type
     */
    public void setType(int type) {
        this.type = type;
    }

    public boolean isDelete() {
        return isDelete;
    }

    public void setDelete(boolean delete) {
        isDelete = delete;
    }

    private void init() {
        setOrientation(OrientationHelper.HORIZONTAL);
        completedColor = ContextCompat.getColor(getContext(), R.color.color_a541c9);
        nextColor = ContextCompat.getColor(getContext(), R.color.color_dddddd);
        setOverScrollMode(OVER_SCROLL_NEVER);

    }

}
